package com.pvdnc.tapkit.chat;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.pvdnc.nsocket.EndPoint;
import com.pvdnc.nsocket.log.AbstractLog;
import com.pvdnc.nsocket.log.SLog;
import com.pvdnc.tapkit.chat.chat.ChatServer;
import com.pvdnc.tapkit.chat.chat.client.ChatStart;
import com.pvdnc.tapkit.chat.chat.client.ServiceStart;
import com.pvdnc.tapkit.chat.ui.ChatActivity;
import com.pvdnc.tapkit.utils.LogImpl;
import com.pvdnc.tapkit.utils.StringUtils;

import java.util.Random;

public class ChatMainActivity extends AppCompatActivity {
    private static final SLog LOG=new SLog(ChatMainActivity.class);

    static{
        AbstractLog.systemReady(new LogImpl());
    }

    private EditText txtHost;
    private EditText txtToName;

    private EditText txtPassword;

    private EditText txtClientName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_main);
        txtHost=findViewById(R.id.txtHost);
        txtToName=findViewById(R.id.txtToName);

        txtPassword=findViewById(R.id.txtPassword);
        txtClientName=findViewById(R.id.txtClientName);

        findViewById(R.id.cmdStartServer).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startServer();
            }
        });
        findViewById(R.id.cmdStartClient).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startChatService();

            }
        });
        findViewById(R.id.cmdStartChat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChatStart start=new ChatStart();
                String selfName=ChatService.get().getName();
                if(selfName==null){
                    LOG.e("self name is null,is client ready ?");
                    return;
                }
                start.mSrcName=selfName;
                start.mToName=txtToName.getText().toString();
                ChatActivity.start(ChatMainActivity.this,start);
            }
        });
    }

    private byte[] getFullEncryptKey(){
        String keyStr= txtPassword.getText().toString();
        if(StringUtils.isNullOrEmpty(keyStr)) {
            LOG.e("fail to get full encrypt key,use default");
            keyStr="password";
        }
        return keyStr.getBytes();
    }

    private String getClientName(){
        String clientName=txtClientName.getText().toString();
        if(StringUtils.isNullOrEmpty(clientName)){
            LOG.e("fail to get client name,use random");
            clientName= "democlient-"+new Random().nextInt(10);
        }
        return clientName;
    }

    private void startChatService(){
        //启动服务
        ServiceStart serviceStart=new ServiceStart();
        serviceStart.mEndPoint=DEFAULT_ENDPOINT;
        serviceStart.mEndPoint.setHost(txtHost.getText().toString());
        serviceStart.mName=getClientName();
        serviceStart.mFullEncryptKey=getFullEncryptKey();
        ChatService.start(this,serviceStart);
    }

    private static final EndPoint DEFAULT_ENDPOINT=new EndPoint(null,8972);

    private void startServer(){
        new Thread("Server"){
            @Override
            public void run() {
                super.run();
                EndPoint endPoint=DEFAULT_ENDPOINT;
                endPoint.setHost(txtHost.getText().toString());
                ChatServer server=new ChatServer(endPoint,getFullEncryptKey());
                server.doBind();
            }
        }.start();
    }
}
